<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: street.php 5677 2011-03-30 01:31:05Z menglingmin $
 */

define("CURSCRIPT", "street");
include_once('./common.php');
require_once(B_ROOT.'./source/function/cache.func.php');

// 分页处理
$tpp = $_G['setting']['shopsearchperpage'];

// 处理 COOKIE 以及外来筛选条件
if (!$_SERVER['QUERY_STRING']) {
	ssetcookie('list_street_filterbycard', "0", -1, 1, true);
	ssetcookie('list_street_filterbyclaimed', "0", -1, 1, true);
	ssetcookie('list_street_sort', '0', -1, 1, true);
} else {
	if ($_G['gp_filter'] == "card") {
		ssetcookie('list_street_filterbycard', "1", 86400*30, 1, true);
	}
}

updatebrandadscache(false, 86430);
$tagarrs = $where = $shoplist = $value = array();
$tagids = $joinsql = $wheresql = '';
$query = NULL;

$viewby = (in_array($_G['cookie']['list_street_view'], array('list', 'pic'))) ? $_G['cookie']['list_street_view'] : 'list';
$sortby = (in_array($_G['cookie']['list_street_sort'], array('view', 'ask', 'remark'))) ? $_G['cookie']['list_street_sort'] : 'default';
$filterbycard = ($_G['cookie']['list_street_filterbycard']) ? true : false;
$filterbyclaimed = ($_G['cookie']['list_street_filterbyclaimed']) ? true : false;

// 常见id处理
foreach(array('itemid', 'nid', 'uid', 'catid', 'tagid') as $value) {
	$_GET[$value] = $_POST[$value] = intval(!empty($_POST[$value])?$_POST[$value]:$_GET[$value]);
}
$keyword = trim($_G['gp_keyword']);

$catid = $_GET['catid'] ? $_GET['catid'] : 0;
$region = $_GET['region'] ? $_GET['region'] : 0;
$regionlist_select = '';
$regionlist = getcategory('region');
if(!empty($region)) {
	$location['region'] = $regionlist[$region]['name'];
	if(!array_key_exists($region, $regionlist)) {
		showmessage('no_thisregion');
	}
}

$categorylist = getcategory('shop');
if(!empty($catid) && !array_key_exists($catid, $categorylist)) {
	showmessage('no_thiscatid');
}
include_once('./batch.attribute.php');
$searchcats = getsearchcats($categorylist, $catid);
$tagid = array();
$tagid[] = $catid;
if(is_array($searchcats)) {
	foreach($searchcats as $key=>$value) {
		$tagid[] = $value['catid'];
	}
}
$tagids = implode(',', $tagid);
!empty($region) ? $where[] = 'i.region IN ('.$regionlist[$region]['subcatid'].')':'';

$all = $thisstreet = '';
$_GET['range'] == 'all' ? $all = 'checked' : $thisstreet = 'checked';

// 条件拼合
$joinsql = ' FROM '.DB::table('shopitems').' s LEFT JOIN '.DB::table('scorestats').' ss ON ss.itemid=s.itemid';

// 提交了搜索数据
$_GET['range'] != 'all' && !empty($tagids) && $where[] = 'i.catid IN ('.$tagids.')';
$where[] = 'i.grade > 2';
$keyword && $where[] = 'i.subject LIKE \'%' . $keyword . '%\'';
$filterbycard && $where[] = 'i.isdiscount = 1';
$filterbyclaimed && $where[] = 'i.claimedshop = 1';
$wheresql = implode(' AND ', $where);

if($sortby == "view") {
	$sql_orderby = ' ORDER BY i.viewnum DESC';
} elseif($sortby == "ask") {
	$sql_orderby = ' ORDER BY i.replynum DESC';
} elseif ($sortby == 'remark') {
	$sql_orderby = ' ORDER BY i.score DESC';
} else {
	$sql_orderby = " ORDER BY i.displayorder ASC, i.itemid DESC";
}

// 查询分类结果
if(empty($catid) || $categorylist[$catid]['havechild']) {
	$_BCACHE->cachesql('catnums', 'SELECT COUNT(i.itemid) as count, i.catid FROM '.DB::table('shopitems').' i WHERE '.$wheresql.' GROUP BY i.catid', 0, 0, 100, 0, 'sitelist', 'shop');

	foreach($_SBLOCK['catnums'] as $value) {
		$catnums[$value['catid']] = intval($value['count']);
	}
	if(is_array($searchcats)) {
		foreach($searchcats as $key=>$value) {
			$catnums = getcatcount($value['catid'], $catnums);
		}
	}
}

// 数据查询，拆分SQL，分片缓存
$_BCACHE->cachesql('shopsearch', 'SELECT i.itemid FROM '.DB::table('shopitems').' i WHERE ' . $wheresql . $sql_orderby, 0, 1, $tpp, 0, 'sitelist', 'shop');
$multipage = $_SBLOCK['shopsearch_multipage'];
$resultcount = $_SBLOCK['shopsearch_listcount'];
foreach($_SBLOCK['shopsearch'] as $value) {
	$value = $_BCACHE->getshopinfo($value['itemid']);
	if ($viewby == "list") {
		$value['summary'] = cutstr($value['subject'], 15, true);
		$value['discount'] = cutstr($value['discount'], 45, true);
	} else {
		$value['summary'] = cutstr($value['subject'], 14, true);
		$value['discount'] = cutstr($value['discount'], 16, true);
	}
	list($value['score_fir'], $value['score_sec']) = explode(".", $value['score']);
	if($viewby == 'list') {
		$value['thumb'] = str_replace('static/image/nophoto.gif', 'static/image/nophotosmall.gif', $value['thumb']);
	}
	$shoplist[] = $value;
}

// 用于模板中显示
$keyword_show = htmlspecialchars($keyword);

$active['street'] = ' class="active"';
$location['name'] = (!$keyword_show ? '' : $keyword_show . ' - ') . $_G['setting']['site_nav']['street']['name'];

// 分类面包屑导航
require_once(B_ROOT.'./source/function/nav.func.php');
$catnavarray = getcatnavarray($catid, 'shop');
$samelevelcatarray = getcategorychild($catid, 'shop', $catid);
$regionnavarray = getregionnavarray($region);

$seo_title = ($catid == 0 ? "" : $_G['categorylist'][$catid]['name'] . " - ") . $location['name'] . " - " . $seo_title;
include template('templates/site/default/street.html.php', 1);

// 正则处理url/模板
ob_out();

?>